'''
Created on Dec 4, 2012

@author: laura
'''
import unittest
from Threatened import Threatened
from ChessConsts import ChessConsts
from ChessBoard import ChessBoard

class Test(unittest.TestCase):
    '''
    Tests for the Threatened module, which is a holder for the isThreatened method.
    Ideally I'd want tests here that  test all the different ways that each piece 
    can threaten, with a couple of permutations of board locations, colours,
    directions etc - not going to get through an exhaustive set of tests in the lab
    time though '''

    def testInitialBoardNoThreat(self):
        board = self.getDefaultBoard()
        self.assertFalse(Threatened.isThreatened(board, 4, 4, ChessConsts.WHITE))


    def testInitialPawnThreats(self):
        board = self.getDefaultBoard()
        # White  is threatened directly in front of the black row of pawns
        self.assertTrue(Threatened.isThreatened(board, 1, 2, ChessConsts.WHITE))    
        # Black is threatened directly in front of the white row of pawns - test edge conditions
        self.assertTrue(Threatened.isThreatened(board, 7, 5, ChessConsts.BLACK))    
        self.assertTrue(Threatened.isThreatened(board, 0, 5, ChessConsts.BLACK))  

        # Check the same squares are actually threatened by pawns        
        self.assertTrue(Threatened.threatenedByPawn(board, 1, 2, ChessConsts.WHITE))    
        self.assertTrue(Threatened.threatenedByPawn(board, 7, 5, ChessConsts.BLACK))    
        self.assertTrue(Threatened.threatenedByPawn(board, 0, 5, ChessConsts.BLACK))   

        # Check we aren't threatened by out own side
        self.assertFalse(Threatened.threatenedByPawn(board, 1, 2, ChessConsts.BLACK))    
        self.assertFalse(Threatened.threatenedByPawn(board, 7, 5, ChessConsts.WHITE))    
        self.assertFalse(Threatened.threatenedByPawn(board, 0, 5, ChessConsts.WHITE))   


    def testInitialKnightThreats(self):
        board = self.getDefaultBoard()
        # Black is threatened in range of white knight
        self.assertTrue(Threatened.isThreatened(board, 0, 5, ChessConsts.BLACK)) 
        self.assertTrue(Threatened.isThreatened(board, 2, 5, ChessConsts.BLACK))
        # White is threatened in range of white knight
        self.assertTrue(Threatened.isThreatened(board, 7, 2, ChessConsts.WHITE)) 
        self.assertTrue(Threatened.isThreatened(board, 5, 2, ChessConsts.WHITE))
        
        # Assert same squares threatened by knights
        self.assertTrue(Threatened.threatenedByKnight(board, 0, 5, ChessConsts.BLACK)) 
        self.assertTrue(Threatened.threatenedByKnight(board, 2, 5, ChessConsts.BLACK))
        self.assertTrue(Threatened.threatenedByKnight(board, 7, 2, ChessConsts.WHITE)) 
        self.assertTrue(Threatened.threatenedByKnight(board, 5, 2, ChessConsts.WHITE))
        
        # Assert not threatened by own side
        self.assertFalse(Threatened.threatenedByKnight(board, 0, 5, ChessConsts.WHITE)) 
        self.assertFalse(Threatened.threatenedByKnight(board, 2, 5, ChessConsts.WHITE))
        self.assertFalse(Threatened.threatenedByKnight(board, 7, 2, ChessConsts.BLACK)) 
        self.assertFalse(Threatened.threatenedByKnight(board, 5, 2, ChessConsts.BLACK))
         

    def testMovePawnForwardToThreaten(self):
        board = self.getDefaultBoard()
         
        # Not threatened... yet
        self.assertFalse(Threatened.isThreatened(board, 2, 4, ChessConsts.WHITE)) 
         
        board[1][2] = '.'
        board[2][2] = 'p'
         
        self.assertTrue(Threatened.isThreatened(board, 3, 3, ChessConsts.WHITE))
        self.assertTrue(Threatened.isThreatened(board, 3, 1, ChessConsts.WHITE))
        
    def testQueenThreat(self):
        board = self.getDefaultBoard()
         
        # Not threatened... yet
        self.assertFalse(Threatened.isThreatened(board, 2, 4, ChessConsts.WHITE)) 
         
        board[1][2] = '.'
        board[2][2] = 'p'
         
        self.assertTrue(Threatened.threatenedByRookBishopOrRoyals(board, 3, 0, ChessConsts.WHITE))
        
    def getDefaultBoard(self):
        ''' Utility to get a chessboard - use ChessBoard for 
            now but it's just 8x8 array of chars'''
        chessboard = ChessBoard()
        chessboard.resetBoard()
        #chessboard.printBoard()
        return chessboard._board

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()